﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Modules.Couple.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.Presenters;
using StockholmsStad.Windows.UI.Modules.Couple.Views;
using StockholmsStad.Windows.UI.Modules.Couple.Models;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Windows.UI.Modules.Couple.Controls;
using System;

namespace StockholmsStad.Windows.UI.Modules.Couple.Controllers
{
    public class CouplesController : ICouplesController
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public CouplesController(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        public virtual void OnCoupleSelected(int ticketId)
        {
            var tabRegion = regionManager.Regions[RegionNames.TabRegion];
            SetSelectedCouple(tabRegion, ticketId);
            SetSelectedBooking(tabRegion, ticketId);
            SetSelectedCeremony(tabRegion, ticketId);
            SetSelectedExtras(tabRegion, ticketId);
            SetSelectedHistory(tabRegion, ticketId);
            
        }

        public void SetSelectedBooking(BookingPresentationModel presentationModel)
        {
            var bookingRegion = regionManager.Regions["BookingRegion"];

            if(bookingRegion != null)
            {
                if(!presentationModel.TicketId.HasValue)
                    presentationModel.TicketId = 0;

                string viewName = string.Format("{0}_{1}",
                                                Enum.GetName(typeof (CeremonyType), presentationModel.CeremonyType),
                                                presentationModel.TicketId);

                if (presentationModel.CeremonyType == CeremonyType.Ordinary)
                {
                    var existingView =
                        bookingRegion.GetView(viewName);

                    var ordinaryBookingPresenter = container.Resolve<OrdinaryBookingPresenter>();
                    if (existingView == null)
                    {
                        bookingRegion.Add(ordinaryBookingPresenter.View, viewName, true);
                        bookingRegion.Activate(ordinaryBookingPresenter.View);
                    }
                    else
                    {
                        var exist = existingView as OrdinaryBookingControl;
                        if (exist != null) exist.Model = presentationModel;
                        bookingRegion.Activate(exist);
                    }
                }
                else 
                {
                    var existingView =
                        bookingRegion.GetView(viewName);

                    var individualBookingPresenter = container.Resolve<IndividualBookingPresenter>();
                    if (existingView == null)
                    {
                        bookingRegion.Add(individualBookingPresenter.View, viewName, true);
                        bookingRegion.Activate(individualBookingPresenter.View);
                    }
                    else
                    {
                        var exist = existingView as IndividualBookingControl;
                        if (exist != null) exist.Model = presentationModel;
                        bookingRegion.Activate(exist);
                    }
                }                
            }
        }

        private void SetSelectedCouple(IRegion tabRegion, int ticketId)
        {
            var existingView = tabRegion.GetView(ViewNames.CoupleTabView);

            if (existingView != null)
            {
                var detailsPresenter = container.Resolve<CoupleDetailsPresenter>();
                detailsPresenter.SetSelectedTicket(ticketId);

                var exist = existingView as TabItemCoupleView;
                if (exist != null) exist.SelectedTicketId = ticketId;
                //tabRegion.Activate(exist);
            }
        }

        private static void SetSelectedBooking(IRegion tabRegion, int ticketId)
        {
            var existingView = tabRegion.GetView(ViewNames.BookingTabView) as TabItemBookingView;

            if (existingView != null)
                existingView.SelectedTicketId = ticketId;
        }

        private static void SetSelectedCeremony(IRegion tabRegion, int ticketId)
        {
            var existingView = tabRegion.GetView(ViewNames.CeremonyTabView) as TabItemCeremonyView;

            if (existingView != null)
            {
                existingView.SelectedTicketId = ticketId;
            }
        }

        private static void SetSelectedExtras(IRegion tabRegion, int ticketId)
        {
            var existingView = tabRegion.GetView(ViewNames.ExtrasTabView) as TabItemExtrasView;

            if (existingView != null)
                existingView.SelectedTicketId = ticketId;
        }

        private static void SetSelectedHistory(IRegion tabRegion, int ticketId)
        {
            var existingView = tabRegion.GetView(ViewNames.HistoryTabView) as TabItemHistoryView;

            if (existingView != null)
                existingView.SelectedTicketId = ticketId;
        }

        #region ICouplesController Members


        #endregion
    }
}
